<?php
/**
 * Created by PhpStorm.
 * User: robot
 * Date: 2018-04-25
 * Time: 15:52
 */

namespace app\pub\controller;


use app\base\Auth;
use app\base\model\User;
use app\base\validate\PubValidate;
use think\Controller;
use think\Request;

class Login extends Controller
{
    /***
     * 登陆页面
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $request = Request::instance();
        if ($request->isAjax()) {
            $data = Request::instance()->param();
            $validate = new PubValidate();
            if (!$validate->scene('login')->check($data)) {
                return ['code'=>-1,'msg'=>$validate->getError()];
            }

            $user = User::get(['user_name' => $data['user_name']]);
            if (is_null($user)) {
                return ['code'=>-1,'msg'=>'账号不存在'];
            }
            if (md5($data['user_pwd'])!=$user->user_pwd) {
                return ['code'=>-1,'msg'=>'密码错误'];
            }
            $remember=isset($data['remember'])?true:false;
            Auth::doLogin($user->hidden(['user_pwd'])->getData(),$remember);
            return ['code' => 1, 'msg' => '登录成功','url'=>'/index'];
        }
        return $this->fetch();
    }
}